<?php
$act=$_GET["act"];


if($act==""){
	include("view/search.php");
}elseif($act=="price"){
	
	$gia=$_POST["gia"];
	$mingia=$gia-1000000;
	
	$query=mysql_query("select * from product where giaban>=$mingia && giaban<$gia");
    $tongItems=mysql_num_rows($query);	
	if($tongItems%10==0){
	   $tongPage=$tongItems/10;
    }else{
	   $tongPage=(int)($tongItems/10+1);
   }
	
	$pageNow=$_GET['pageNow'];
	
	if($pageNow==""||$pageNow<1){
		$pageNow=1;
	}
	if($pageNow>$tongPage){
		$pageNow=$tongPage;
	}
	
	$start=($pageNow-1)*10;
	$num=10;
	
	include("view/search.php");	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<p align='center'>";
  
if($tongPage>1){
	
if($pageNow==1){
	    echo "|<<&nbsp;<";
	}else{
		echo "<a href='index.php?pageNow=1'> |<< </a> &nbsp;";
		echo "<a href='index.php?pageNow=".($pageNow-1)."'> < </a>";
    }

$i=$pageNow;
if($i<=$tongPage-5){
	while($i<$pageNow+5){
		if($i==$pageNow){
		    echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
		}else{
			echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
		}
		$i++;
	}
}else{
	if($tongPage<=5){
		$i=1;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}else{
		$i=$tongPage-5;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}
}

	if($pageNow==$tongPage){
		echo ">&nbsp;>>|";
	}else{
		echo "<a href='index.php?pageNow=".($pageNow+1)."'> > </a> &nbsp;";
		echo "<a href='index.php?pageNow=$tongPage'> >>| </a>";	
	} 
}

echo "</p>";
	
	
	$sql=mysql_query("select masp,tensp,giaban,hinhanh from product where giaban>=$mingia && giaban<$gia limit $start,$num");
	if($sql){
		$t=mysql_num_rows($sql);
		$i=1;
		while(($row=mysql_fetch_row($sql))&&($i<=$t)){
			$i++;	
			$sql=mysql_query("select* from watchingproduct where masp='$row[0]'");
			$sl=mysql_fetch_array($sql);

?>				
			<div class="item">
				<img src="images/bordtop.gif" alt="" width="274" height="2" />
				<div>
					<img src="images/sanpham/<?php echo $row[3]; ?>" alt="" width="102" height="88" class="items" />
					<h1><?php echo $row[1]; ?></h1>
					<span><?php if($sl['soluongcon']==0){
									echo "Đã hết hàng";
								}else{
									echo "<b size='8'>Hiện còn:    </b>
									<font color=blue size='4'>".$sl['soluongcon']."</font>    <b>&nbsp;&nbsp;chiếc</b>";
								}?> 
                     </span>
					<p><strong><?php echo $row[2]."VNĐ"; ?></strong></p>
					<a href="index.php?page=detailproduct&&id=<?php echo $row[0]; ?>"><img src="images/button1.gif" alt="" width="129" height="20"/></a>
					<a href="index.php?page=cart&&act=buy&&masp=<?php echo $row[0]; ?>"><img src="images/button2.gif" alt="" width="127" height="20" /></a>
				</div>
				<img src="images/bordbot.gif" alt="" width="274" height="2" />
			</div>
            
		<?php if($i%2) {echo "<br/>";}
		}
	}else{
		echo "<center><br>Xin lỗi, không tìm thấy sản phẩm mà bạn cần trong dữ liệu!<br><br>"; 
	}?>

<?php 
}elseif($act=="advandce"){
	$tensp=$_POST['txtsanpham'];
	$hang=$_POST['hangsx'];
	
	$query=mysql_query("select * from product where tensp like('$tensp%')&& mahsx='$hang'");
    $tongItems=mysql_num_rows($query);	
	if($tongItems%10==0){
	   $tongPage=$tongItems/10;
    }else{
	   $tongPage=(int)($tongItems/10+1);
   }
	
	$pageNow=$_GET['pageNow'];
	
	if($pageNow==""||$pageNow<1){
		$pageNow=1;
	}
	if($pageNow>$tongPage){
		$pageNow=$tongPage;
	}
	
	$start=($pageNow-1)*10;
	$num=10;

	include("view/search.php");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<p align='center'>";
   
if($tongPage>1){
	
if($pageNow==1){
	    echo "|<<&nbsp;<";
	}else{
		echo "<a href='index.php?pageNow=1'> |<< </a> &nbsp;";
		echo "<a href='index.php?pageNow=".($pageNow-1)."'> < </a>";
    }

$i=$pageNow;
if($i<=$tongPage-5){
	while($i<$pageNow+5){
		if($i==$pageNow){
		    echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
		}else{
			echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
		}
		$i++;
	}
}else{
	if($tongPage<=5){
		$i=1;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}else{
		$i=$tongPage-5;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}
}

	if($pageNow==$tongPage){
		echo ">&nbsp;>>|";
	}else{
		echo "<a href='index.php?pageNow=".($pageNow+1)."'> > </a> &nbsp;";
		echo "<a href='index.php?pageNow=$tongPage'> >>| </a>";	
	} 
}

echo "</p>";
	
	
	$sql=mysql_query("select masp,tensp,giaban,hinhanh from product where tensp like('$tensp%')&&mahsx='$hang' limit $start,$num");
	if($sql){
		$t=mysql_num_rows($sql);
		$i=1;
		
		while(($row=mysql_fetch_row($sql))&&($i<=$t)){
			$i++;	
			$sql1=mysql_query("select* from watchingproduct where masp='$row[0]'");
			$sl=mysql_fetch_array($sql1);

?>				
			
	<div class="item">
				<img src="images/bordtop.gif" alt="" width="274" height="2" />
				<div>
					<img src="images/sanpham/<?php echo $row[3]; ?>" alt="" width="102" height="88" class="items" />
					<h1><?php echo $row[1]; ?></h1>
					<span><?php if($sl['soluongcon']==0){
									echo "Đã hết hàng";
								}else{
									echo "<b size='8'>Hiện còn:    </b>
									<font color=blue size='4'>".$sl['soluongcon']."</font>    <b>&nbsp;&nbsp;chiếc</b>";
								}?> 
                     </span>
					<p><strong><?php echo $row[2]."VNĐ"; ?></strong></p>
					<a href="index.php?page=detailproduct&&id=<?php echo $row[0]; ?>"><img src="images/button1.gif" alt="" width="129" height="20"/></a>
					<a href="index.php?page=cart&&act=buy&&masp=<?php echo $row[0]; ?>"><img src="images/button2.gif" alt="" width="127" height="20" /></a>
				</div>
				<img src="images/bordbot.gif" alt="" width="274" height="2" />
			</div>
            
		<?php if($i%2) {echo "<br/>";}
		}
	}else{
		 echo "<center><br>Xin lỗi, không tìm thấy sản phẩm mà bạn cần trong dữ liệu!<br><br>"; 
	}?>
		   

<?php 	
}elseif($act=="mahsx"){
	$mahsx=$_GET['id'];
	
	$query=mysql_query("select * from product where mahsx='$mahsx'");
    $tongItems=mysql_num_rows($query);	
	if($tongItems%10==0){
	   $tongPage=$tongItems/10;
    }else{
	   $tongPage=(int)($tongItems/10+1);
   }
	
	$pageNow=$_GET['pageNow'];
	
	if($pageNow==""||$pageNow<1){
		$pageNow=1;
	}
	if($pageNow>$tongPage){
		$pageNow=$tongPage;
	}
	
	$start=($pageNow-1)*10;
	$num=10;
	
	include("view/search.php");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<p align='center'>";
   
if($tongPage>1){
	
if($pageNow==1){
	    echo "|<<&nbsp;<";
	}else{
		echo "<a href='index.php?pageNow=1'> |<< </a> &nbsp;";
		echo "<a href='index.php?pageNow=".($pageNow-1)."'> < </a>";
    }

$i=$pageNow;
if($i<=$tongPage-5){
	while($i<$pageNow+5){
		if($i==$pageNow){
		    echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
		}else{
			echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
		}
		$i++;
	}
}else{
	if($tongPage<=5){
		$i=1;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}else{
		$i=$tongPage-5;
		while($i<=$tongPage){
			if($i==$pageNow){
		    	echo "&nbsp;&nbsp;<font color='red'><b><a href='index.php?pageNow=".$i."'>".$i."</a></b></font>&nbsp;&nbsp;";
			}else{
				echo "&nbsp;&nbsp;<a href='index.php?pageNow=".$i."'>".$i."</a>&nbsp;&nbsp;";
			}
			$i++;
		}
	}
}

	if($pageNow==$tongPage){
		echo ">&nbsp;>>|";
	}else{
		echo "<a href='index.php?pageNow=".($pageNow+1)."'> > </a> &nbsp;";
		echo "<a href='index.php?pageNow=$tongPage'> >>| </a>";	
	} 
}

echo "</p>";
	
	$sql=mysql_query("select masp,tensp,giaban,hinhanh from product where mahsx='$mahsx' limit $start,$num");
	if($sql){
		$t=mysql_num_rows($sql);
		$i=1;
		
		while(($row=mysql_fetch_row($sql))&&($i<=$t)){
			$i++;	
			$sql1=mysql_query("select* from watchingproduct where masp='$row[0]'");
			$sl=mysql_fetch_array($sql1);

?>				
			
			<div class="item">
				<img src="images/bordtop.gif" alt="" width="274" height="2" />
				<div>
					<img src="images/sanpham/<?php echo $row[3]; ?>" alt="" width="102" height="88" class="items" />
					<h1><?php echo $row[1]; ?></h1>
					<span><?php if($sl['soluongcon']==0){
									echo "Đã hết hàng";
								}else{
									echo "<b size='8'>Hiện còn:    </b>
									<font color=blue size='4'>".$sl['soluongcon']."</font>    <b>&nbsp;&nbsp;chiếc</b>";
								}?> 
                     </span>
					<p><strong><?php echo $row[2]."VNĐ"; ?></strong></p>
					<a href="index.php?page=product&&act=detail&&masp=<?php echo $row[0]; ?>"><img src="images/button1.gif" alt="" width="129" height="20"/></a>
					<a href="index.php?page=cart&&act=buy&&masp=<?php echo $row[0]; ?>"><img src="images/button2.gif" alt="" width="127" height="20" /></a>
				</div>
				<img src="images/bordbot.gif" alt="" width="274" height="2" />
			</div>
            
		<?php if($i%2) {echo "<br/>";}
		}
	}else{
		 echo "<center><br>Xin lỗi, không tìm thấy sản phẩm mà bạn cần trong dữ liệu!<br><br>"; 
	}?>
<br/>
  
<?php 
}
?>